package com.xdf.swids.controller;

import com.xdf.swids.datasource.DynamicDataSource;
import com.xdf.swids.model.TenantInfo;
import com.xdf.swids.service.ITenantInfoService;
import com.xdf.swids.utils.SpringContextUtils;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/tenantInfo")
public class TenantInfoController {
    @Autowired
    private ITenantInfoService service;

    @GetMapping("/all")
    public List<TenantInfo> selectAll(){
        return service.selectAll();
    }

    @GetMapping("/save")
    public Boolean saveInfo() {
        TenantInfo tenantInfo = new TenantInfo();
        tenantInfo.setTenant_name("动态新增");
        tenantInfo.setDatasource_url("jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=Asia/Shanghai&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull");
        tenantInfo.setDatasource_username("root");
        tenantInfo.setDatasource_password("root");
        tenantInfo.setDatasource_driver("com.mysql.cj.jdbc.Driver");
        tenantInfo.setStatus(1);
        tenantInfo.setCreate_time(new Date(System.currentTimeMillis()));
        tenantInfo.setUpdate_time(new Date(System.currentTimeMillis()));
        boolean b = service.save(tenantInfo)>0 ? true : false;
        if (b) {
            log.info("======初始化动态数据源=====");
            DynamicDataSource dynamicDataSource = (DynamicDataSource) SpringContextUtils.getBean("dynamicDataSource");
            HikariDataSource master = (HikariDataSource) SpringContextUtils.getBean("master");
            Map<Object, Object> dataSourceMap = new HashMap<>();
            dataSourceMap.put("master", master);
            List<TenantInfo> tenantList = service.selectAll();
            for (TenantInfo tenantInfos : tenantList) {
                log.info(tenantInfos.getTenant_id() + "     " + tenantInfos.getTenant_name());
                HikariDataSource dataSource = new HikariDataSource();
                dataSource.setDriverClassName(tenantInfos.getDatasource_driver());
                dataSource.setJdbcUrl(tenantInfos.getDatasource_url());
                dataSource.setUsername(tenantInfos.getDatasource_username());
                dataSource.setPassword(tenantInfos.getDatasource_password());
                dataSource.setDataSourceProperties(master.getDataSourceProperties());
                dataSourceMap.put(tenantInfos.getTenant_id(), dataSource);
            }
            // 设置数据源
            dynamicDataSource.setDataSources(dataSourceMap);
            /**
             * 必须执行此操作，才会重新初始化AbstractRoutingDataSource 中的 resolvedDataSources，也只有这样，动态切换才会起效
             */
            dynamicDataSource.afterPropertiesSet();
        }
        return b;
    }

}
